import 'package:dio/dio.dart';
import 'package:hm_show_flutter/constant/string.dart';
import 'package:hm_show_flutter/entity/category_list_entity.dart';
import 'package:hm_show_flutter/entity/category_parent_entity.dart';
import 'package:hm_show_flutter/entity/goods_list_entity.dart';
import 'package:hm_show_flutter/utils/http_util.dart';
import 'package:hm_show_flutter/api/api.dart';

typedef OnFail(String message);

typedef OnSuccess<T>(T data);

int parentId; //分类id
int companyId; //门店id

class CategoryService {
  Future getCategoryParentData(OnSuccess onSuccess,
      {OnFail onFail, int parentId, int companyId}) async {
    try {
      FormData data = new FormData();
      if (parentId != null) {
        data.add("parentId", parentId);
      }
      if (companyId != null) {
        data.add("companyId", companyId);
      }

      var response =
          await HttpUtil.instance.get(Api.CATEGORY, parameters: data);
      if (response['errno'] == 0) {
        CategoryParentEntity entity = CategoryParentEntity.fromJson(response);
        onSuccess(entity);
      } else {
        onFail(response['errmsg']);
      }
    } catch (e) {
      print(e);
      onFail(Strings.SERVER_EXCEPTION);
    }
  }

  Future getCategoryListData(int page,int categoryId,OnSuccess onSuccess,
      {OnFail onFail}) async {
    try {
      FormData data = new FormData();
      data.add("categoryId", categoryId);

      var response =
          await HttpUtil.instance.get(Api.GOODS_LIST, parameters: data);
      if (response['errno'] == 0) {
        GoodsListEntity entity = GoodsListEntity.fromJson(response);
        onSuccess(entity);
      } else {
        onFail(response['errmsg']);
      }
    } catch (e) {
      print(e);
      onFail(Strings.SERVER_EXCEPTION);
    }
  }
}
